import Vue from 'vue'
import Vuex from 'vuex'
//使用Vuex
Vue.use(Vuex);
//准备actions对象——响应组件中用户的动作
const actions = {
    jiaOdd(context, value) {
        // console.log(context, value)
        if (context.state.sum % 2) {
            context.commit('JIA', value);
        }
    },
    jiaWait(context, value) {
        setTimeout(() => {
            context.commit('JIA', value)
        }, 500)
    }
}
// 准备matations对象——操作state中的数据
const mutations = {
    JIA(state, value) {
        state.sum += value
    },
    JIAN(state, value) {
        state.sum -= value
    },
    ADD_PERSON(state, value) {
        state.personList.unshift(value);
    }
}
//准备state对象——用于存储数据
const state = {
    sum: 0,
    school: "尚硅谷",
    subject: "前端",
    personList: [
        { id: "001", name: "张三" }
    ]
}
//准备getters——用于将state中的数据进行加工
const getters = {
    bigSum(state) {
        return state.sum * 10
    }
}

//创建并暴露store
export default new Vuex.Store({
    actions,
    mutations,
    state,
    getters
})